<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/11/24 0024
  Time: 9:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户编辑</title>
    <jsp:include page="../template/bootstrap_common.jsp"></jsp:include>
</head>
<body>
<div class="row1">
    <%-- 工具栏--%>
    <ul class="breadcrumb">
        <li><a href="${pageContext.request.contextPath}/wellcome.jsp">主页</a></li>
        <li>用户编辑</li>
    </ul>
    <br/>
    <div>
        <div style="margin: 0 auto; width: 400px;height: 500px;">
            <div class="row">
                <div>
                    <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                        <input type="text" class="form-control" placeholder="请输入账号" id="input" name="input">
                        <div style="margin-top: 10px; text-align: center">
                            <button  style="margin-right: 20px;" type="button" class="btn btn-primary" id="queryUserByAccount">查询<span
                                    class="glyphicon glyphicon-search"></span>
                            </button><button style="margin-left: 20px;" type="button" class="btn btn-primary" id="deleteUserByAccount">删除<span
                                class="glyphicon glyphicon-trash"></span></button>
                        </div>
                    </div>
                    <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                        <input type="hidden" class="form-control" id="userId" autocomplete="off" name="userId">
                    </div>
                    <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                        账 户:
                        <input type="text"  class="form-control" id="account" name="account">
                    </div>
                    <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                        用户名:
                        <input  type="text" class="form-control"id="username" name="username">
                    </div>
                    <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                        密 码:
                        <input type="password" class="form-control" id="password" name="password">
                    </div>
                </div>
                <div>
                    <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                        <button type="button" id="btnSubmit" class="btn btn-primary form-control">修改</button>
                    </div>
                    <div id="message" class="form-group" style="margin-left: 20px;margin-right: 20px;">

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {

        $("#queryUserByAccount").click(queryUserByAccount)

        $("#deleteUserByAccount").click(deleteUserByAccount)

        $("#btnSubmit").click(updateUser)

    });
    function updateUser() {
        //获取输入框对象
        var account1 = $("#account").val();
        var username1 = $("#username").val();
        var password1 = $("#password").val();
        var userId1 = $("#userId").val();

        var obj = {};
        obj.account = account1;
        obj.userName = username1;
        obj.password = password1;
        obj.userId = userId1;
        // alert(JSON.stringify(obj))
        $.ajax({
            url:"${pageContext.request.contextPath}/user/updateUser",
            type:"POST",
            contentType:"application/json",//传递给前端数据的类型
            data:JSON.stringify(obj),//json数据
            dataType:'json',//后台响应给前台的数据类型
            success:function (resp) {
                $("#message").html("");
                if(resp.message>0){
                    console.log("成功")
                    var message = $("#message");
                    var mes = $("<div>");
                    mes.html('<div class="alert alert-success alert-dismissible" role="alert">\n' +
                        '  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                        '  修改成功！！！\n' +
                        '</div>')
                    message.append(mes);
                }else{
                    console.log("失败")
                    var message = $("#message");
                    var mes = $("<div>");
                    mes.html('<div class="alert alert-danger alert-dismissible" role="alert">\n' +
                        '  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                        '  账号重复 ， 请重新输入！！！\n' +
                        '</div>')
                    message.append(mes);
                }
            }
        })
    }

    function queryUserByAccount() {
        //获取输入框对象   account  输入的
        var account1 = $("#input").val();

        var obj = {};
        obj.account = account1;

        // alert(JSON.stringify(obj))
        $.ajax({
            url:"${pageContext.request.contextPath}/user/queryUserByAccount",
            type:"POST",
            contentType:"application/json",//传递给前端数据的类型
            data:JSON.stringify(obj),//json数据
            dataType:'json',//后台响应给前台的数据类型
            success:function (resp) {
                $("#message").html("");
                if(resp.message>0){
                    var data = resp.data;
                    console.log(data)
                    $('#account').val(data.account);
                    $('#userId').val(data.userId);
                    $('#username').val(data.userName);
                    $('#password').val(data.password);
                    var message = $("#message");
                    var mes = $("<div>");
                    mes.html('<div class="alert alert-success alert-dismissible" role="alert">\n' +
                        '  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                        '  查询成功！！！\n' +
                        '</div>')
                    message.append(mes);
                }else{
                    var message = $("#message");
                    var mes = $("<div>");
                    mes.html('<div class="alert alert-success alert-dismissible" role="alert">\n' +
                        '  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                        '  没有这个用户！！！\n' +
                        '</div>')
                    message.append(mes);
                }
            }
        })
    }
    function deleteUserByAccount() {
        //获取输入框对象   account  输入的
        var account1 = $("#input").val();

        var obj = {};
        obj.account = account1;

        // alert(JSON.stringify(obj))
        $.ajax({
            url:"${pageContext.request.contextPath}/user/deleteUserByAccount",
            type:"POST",
            contentType:"application/json",//传递给前端数据的类型
            data:JSON.stringify(obj),//json数据
            dataType:'json',//后台响应给前台的数据类型
            success:function (resp) {
                $("#message").html("");
                if(resp.message>0){
                    var message = $("#message");
                    var mes = $("<div>");
                    mes.html('<div class="alert alert-success alert-dismissible" role="alert">\n' +
                        '  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                        '  删除成功！！！\n' +
                        '</div>')
                    message.append(mes);
                }else{
                    var message = $("#message");
                    var mes = $("<div>");
                    mes.html('<div class="alert alert-success alert-dismissible" role="alert">\n' +
                        '  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                        '  没有这个用户 , 无法删除！！！\n' +
                        '</div>')
                    message.append(mes);
                }
            }
        })
    }
</script>
</body>
</html>
